/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package comandaXM.vista.manteniment.proveidor;
import comandaXM.accions.ProveidorsAct;
import comandaXM.classes.manteniment.proveidor.Notificacio;
import comandaXM.excepcions.ExceptionHibernate;
import comandaXM.utils.JOPtionPaneXM;
import comandaXM.utils.JTextCompleterXM;
import comandaXM.utils.TDSLanguageUtils;
import comandaXM.utils.Utils;
import comandaXM.utils.ValidarText;
import java.awt.KeyboardFocusManager;
import javax.swing.JLabel;
import javax.swing.JTextArea;



/**
 *
 * @author xmarti
 */
public class JDNotificacio extends javax.swing.JDialog {    
    private Notificacio notificacioActual;  
    private ProveidorsAct proveidorAct= new ProveidorsAct(); 
    private JOPtionPaneXM optionPane= new JOPtionPaneXM(); 
    private boolean actualitzar=false; 
    public JDNotificacio(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setComponents(); 
        this.notificacioActual= new Notificacio();
        this.setTitle("null");
        resetControls() ;
    }
    public JDNotificacio(java.awt.Frame parent, boolean modal,Notificacio notificacio){
        super(parent, modal);
        initComponents();
        this.notificacioActual= notificacio;
        if(notificacioActual.getId()<=0 ) {
            this.setTitle(notificacio.getProveidor() +". "+ TDSLanguageUtils.getMessage("afegirNova"));
        }else{
            this.setTitle(notificacioActual +". "+ TDSLanguageUtils.getMessage("modificar"));
            
        }
        resetControls() ;
        setComponents();         
    }
    
    private void setComponents(){      
         this.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,Utils.forwardKeys());       
         this.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS,Utils.backwardKeys() );                          
         this.txtDescripcio.setDocument(new ValidarText(499));
         setData(); 
    }
    private void setData(){
        this.lblId.setText (String.valueOf(this.notificacioActual.getId())) ;  
        this.txtDescripcio.setText(this.notificacioActual.getDescripcio());         
        this.xecActiu.setSelected(this.notificacioActual.isActiva());
        this.txtDescripcio.requestFocus();
    }
    private void getData() {        
        this.notificacioActual.setDescripcio(this.txtDescripcio.getText());
        this.notificacioActual.setActiva(xecActiu.isSelected());
    }
    private void resetControls(){
        this.lblErrorDescripcio.setVisible(false);
    }
    private boolean validateControls (){
        int estat=0; 
        estat= setValidateJText(this.txtDescripcio,this.lblErrorDescripcio);         
        if (estat>0){
            lblError.setText(TDSLanguageUtils.getMessage("campsObligatoris"));
            return false; 
        }            
        return true;  
    }
    private int   setValidateJText(JTextArea t, JLabel l ) {
        int estat=0; 
        if (t.getText().length()== 0)  {
            t.setBorder(Utils.setBorderError());
            l.setVisible(true );
            estat=1; 
        }else{
            t.setBorder(Utils.setBorderOk());
            l.setVisible(false);
            estat= 0; 
        }
        return estat; 
    }
    private int  setValidateJText(JTextCompleterXM t, JLabel l ) {
        int estat=0 ; 
        if (t.getSelectedItem()== null)  {
            t.getJText().setBorder(Utils.setBorderError());
            l.setVisible(true );
            estat=1; 
        }else{
            t.getJText().setBorder(Utils.setBorderOk());
            l.setVisible(false);
            estat= 0; 
        }
        return estat; 
    }
    
    public boolean isActualitzar() {
        return actualitzar;
    }

    
    
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButtonSave = new javax.swing.JButton();
        jButtonExit = new javax.swing.JButton();
        lblCaptionId = new javax.swing.JLabel();
        lblId = new javax.swing.JLabel();
        lblCaptionDepartament = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtDescripcio = new javax.swing.JTextArea();
        lblError = new javax.swing.JLabel();
        xecActiu = new javax.swing.JCheckBox();
        lblErrorDescripcio = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jButtonSave.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        jButtonSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/comandaXM/images/BotoGuardar.png"))); // NOI18N
        jButtonSave.setMnemonic('G');
        jButtonSave.setText(TDSLanguageUtils.getMessage("jDialogXM.JButton.save.caption"));
        jButtonSave.setToolTipText(TDSLanguageUtils.getMessage("jDialogXM.JButton.save.toolTipText"));
        jButtonSave.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButtonSave.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButtonSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSaveActionPerformed(evt);
            }
        });

        jButtonExit.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        jButtonExit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/comandaXM/images/BotoSortir.png"))); // NOI18N
        jButtonExit.setMnemonic('S');
        jButtonExit.setText(TDSLanguageUtils.getMessage("jDialogXM.JButton.exit.caption"));
        jButtonExit.setToolTipText(TDSLanguageUtils.getMessage("jDialogXM.JButton.exit.toolTipText"));
        jButtonExit.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButtonExit.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButtonExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonExitActionPerformed(evt);
            }
        });

        lblCaptionId.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lblCaptionId.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblCaptionId.setText(TDSLanguageUtils.getMessage("jdNotificacio.lblID.setText"));

        lblId.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lblId.setForeground(java.awt.SystemColor.activeCaption);
        lblId.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        lblId.setText("ID:");

        lblCaptionDepartament.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lblCaptionDepartament.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblCaptionDepartament.setText(TDSLanguageUtils.getMessage("jdNotificacio.lblDescripcio.setText"));

        jScrollPane1.setForeground(java.awt.SystemColor.activeCaption);

        txtDescripcio.setBackground(new java.awt.Color(255, 255, 204));
        txtDescripcio.setColumns(20);
        txtDescripcio.setRows(3);
        jScrollPane1.setViewportView(txtDescripcio);

        lblError.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lblError.setForeground(java.awt.Color.blue);
        lblError.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

        xecActiu.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        xecActiu.setText(TDSLanguageUtils.getMessage("jdNotificacio.actiu.setText"));
        xecActiu.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);

        lblErrorDescripcio.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lblErrorDescripcio.setForeground(java.awt.Color.red);
        lblErrorDescripcio.setText(TDSLanguageUtils.getMessage("campObligatori"));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(83, 83, 83)
                .addComponent(jButtonSave, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButtonExit, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(41, 41, 41))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(13, 13, 13)
                        .addComponent(lblCaptionId, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(8, 8, 8)
                        .addComponent(lblId, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(79, 79, 79)
                        .addComponent(xecActiu, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(lblError, javax.swing.GroupLayout.PREFERRED_SIZE, 475, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(13, 13, 13)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblErrorDescripcio, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblCaptionDepartament, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 400, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButtonSave)
                    .addComponent(jButtonExit))
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblCaptionId)
                    .addComponent(lblId)
                    .addComponent(lblErrorDescripcio))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblCaptionDepartament)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(xecActiu)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblError, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSaveActionPerformed
         if (validateControls()){
            try {
                getData();
                if (notificacioActual.getId()>0 ){
                    this.proveidorAct.update(notificacioActual); 
                     
                }else {
                    notificacioActual.setId(this.proveidorAct.add(notificacioActual));                    
                }            
                this.actualitzar=true; 
                this.dispose();
            } catch (ExceptionHibernate ex) {
                optionPane.errMessagge(ex.getMessage());
            }
         } 
    }//GEN-LAST:event_jButtonSaveActionPerformed

    private void jButtonExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonExitActionPerformed
        this.actualitzar=false; 
        this.dispose();
    }//GEN-LAST:event_jButtonExitActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JDNotificacio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JDNotificacio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JDNotificacio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JDNotificacio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                JDNotificacio dialog = new JDNotificacio(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonExit;
    private javax.swing.JButton jButtonSave;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblCaptionDepartament;
    private javax.swing.JLabel lblCaptionId;
    private javax.swing.JLabel lblError;
    private javax.swing.JLabel lblErrorDescripcio;
    private javax.swing.JLabel lblId;
    private javax.swing.JTextArea txtDescripcio;
    private javax.swing.JCheckBox xecActiu;
    // End of variables declaration//GEN-END:variables
}
